# coding=utf-8

import os
import tensorflow as tf
from PIL import Image
import numpy as np

class analyst(object):
    def __init__(self):
        pass

    def rotateImAndTranslateTest(self):
        # 图像旋转
        def rotateImAndTranslate(im):
            angle = (3 - np.random.randint(7)) * 5
            if angle == 0:
                return np.array(im)
            else:
                im_array = np.array(im.rotate(angle))
                for r in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + [18, 19, 20, 21, 22, 23, 24, 25, 26, 27]:
                    for c in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + [18, 19, 20, 21, 22, 23, 24, 25, 26, 27]:
                        if im_array[r][c] == 0:
                            im_array[r][c] = 255
                return im_array

        def rotateImAndTranslate2(im):
            angle = 0
            if angle == 0:
                return np.array(im)
            else:
                im_array = 255 - np.array(im)
                im2 = Image.fromarray(im_array)
                im_array2 = 255 - np.array(im2.rotate(angle))
                return im_array2

        file_path = "/usr/data/比赛试题/比赛数据"
        # trade_all_labels = []
        # with open(os.path.join(file_path, 'train', 'train_labels.txt')) as f:
        #     for i in f:
        #         trade_all_labels.append(int(i))
        #
        # len([i for i in trade_all_labels if i == 0])

        im = Image.open(os.path.join(file_path, 'train', 'TrainImage', 'TestImage_%d.bmp' % (10 + 1)))


        # im_array = np.array(im)
        # print(im_array.shape)
        # print(im_array)

        im_array2 = rotateImAndTranslate2(im)
        print(im_array2.shape)
        print(im_array2)

    def testCombine(self):
        label_path = '/home/yanghb'
        labels = []
        for i in range(6):
            in_labels = []
            with open(os.path.join(label_path, 'test_labels%d.txt' % (i+1))) as f:
                for j in f:
                    in_labels.append(int(j))
            labels.append(in_labels)

        for i in range(len(labels[0])):
            label_map = {}
            out = 'i=%d' % (i + 1)
            for j in [5, 1, 3]:
                v = labels[j][i]
                out = out + ('\t%d' % v)
                if v in label_map:
                    label_map[v] = label_map[v] + 1
                else:
                    label_map[v] = 1
            if len(label_map) > 1:
                m = 0
                r = -1
                for k in label_map.keys():
                    if label_map[k] > m:
                        m = label_map[k]
                        r = k
                if m == 1:
                    r = labels[5][i]
                print(out, '\t%d' % r)

    def printCombine(self):
        label_path = '/home/yanghb'
        labels = []
        for i in range(6):
            in_labels = []
            with open(os.path.join(label_path, 'test_labels%d.txt' % (i+1))) as f:
                for j in f:
                    in_labels.append(int(j))
            labels.append(in_labels)

        for i in range(len(labels[0])):
            label_map = {}
            out = 'i=%d' % (i+1)
            for j in [5, 1, 3]:
                v = labels[j][i]
                out = out + ('\t%d' % v)
                if v in label_map:
                    label_map[v] = label_map[v] + 1
                else:
                    label_map[v] = 1
            if len(label_map) > 0:
                m = 0
                r = -1
                for k in label_map.keys():
                    if label_map[k] > m:
                        m = label_map[k]
                        r = k
                if m == 1:
                    r = labels[5][i]
                print(out, '\t%d' % r)

if __name__ == '__main__':
    analys = analyst()
    analys.testCombine()


